Add new ImageBitmapRenderingContext tests (#4167) Adding new tests based on this specification: https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context
diff --git a/imagebitmap-renderingcontext/bitmaprenderer-as-imagesource.html b/imagebitmap-renderingcontext/bitmaprenderer-as-imagesource.html new file mode 100644 index 0000000..1dbfa2b --- /dev/null +++ b/imagebitmap-renderingcontext/bitmaprenderer-as-imagesource.html
@@ -0,0 +1,90 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Canvas's ImageBitmapRenderingContext test</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context"> +<script> +var width = 10; +var height = 10; + +function testCanvas(ctx, x, y, r, g, b, a) +{ + var color = ctx.getImageData(x, y, 1, 1).data; + assert_array_equals(color, [r, g, b, a]); +} + +function consumeImageBitmap(image, t) +{ + var myCanvas = document.createElement('canvas'); + myCanvas.width = myCanvas.height = 20; + var myCtx = myCanvas.getContext('bitmaprenderer'); + myCtx.transferFromImageBitmap(image); + + createImageBitmap(myCanvas).then(t.step_func_done(function(imageBitmap) { + // Per spec, when transferFromImageBitmap happens, the transferred + // ImageBitmap (|image| here) should be the intrinsic size of + // myCanvas, and hence myCanvas.width/height is ignored. Therefore, + // this created |imageBitmap| should have the same size as the |image|. + assert_equals(imageBitmap.width, width); + assert_equals(imageBitmap.height, height); + + var dstCanvas = document.createElement('canvas'); + dstCanvas.width = dstCanvas.height = 20; + var dstCtx = dstCanvas.getContext('2d'); + dstCtx.drawImage(myCanvas, 0, 0); + testCanvas(dstCtx, 5, 5, 0, 255, 0, 255); + testCanvas(dstCtx, 15, 15, 0, 0, 0, 0); + })); +} + +async_test(function(t) { + var canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + var ctx = canvas.getContext('2d'); + ctx.fillStyle = '#0f0'; + ctx.fillRect(0, 0, width, height); + createImageBitmap(canvas).then(t.step_func(function(image) { + consumeImageBitmap(image, t); + })); +}, 'Test that createImageBitmap from a bitmaprenderer canvas produces correct result'); + +async_test(function(t) { + var canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + var ctx = canvas.getContext('bitmaprenderer'); + createImageBitmap(canvas).then(t.step_func_done(function(image) { + assert_equals(image.width, width); + assert_equals(image.height, height); + + var dstCanvas = document.createElement('canvas'); + dstCanvas.width = width; + dstCanvas.height = height; + var dstCtx = dstCanvas.getContext('2d'); + dstCtx.drawImage(canvas, 0, 0); + testCanvas(dstCtx, 5, 5, 0, 0, 0, 0); + })); +}, 'Test that createImageBitmap on a bitmaprenderer canvas that never consumes any source produces correct result'); + + +async_test(function(t) { + var canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + var ctx = canvas.getContext('bitmaprenderer'); + ctx.transferFromImageBitmap(null); + createImageBitmap(canvas).then(t.step_func_done(function(image) { + assert_equals(image.width, width); + assert_equals(image.height, height); + + var dstCanvas = document.createElement('canvas'); + dstCanvas.width = width; + dstCanvas.height = height; + var dstCtx = dstCanvas.getContext('2d'); + dstCtx.drawImage(canvas, 0, 0); + testCanvas(dstCtx, 5, 5, 0, 0, 0, 0); + })); +}, 'Test that createImageBitmap on a bitmaprenderer canvas that consumes null produces correct result'); +</script> diff --git a/imagebitmap-renderingcontext/context-creation-with-alpha.html b/imagebitmap-renderingcontext/context-creation-with-alpha.html new file mode 100644 index 0000000..88d12d1 --- /dev/null +++ b/imagebitmap-renderingcontext/context-creation-with-alpha.html
@@ -0,0 +1,75 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Canvas's ImageBitmapRenderingContext test</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context"> +<script> +var width = 10; +var height = 10; + +function testCanvas(ctx, r, g, b, a) +{ + var color = ctx.getImageData(5, 5, 1, 1).data; + assert_array_equals(color, [r, g, b, a]); +} + +function consumeImageBitmap(image, alphaVal, expectedR, expectedG, expectedB, expectedA) +{ + var dstCanvas = document.createElement('canvas'); + dstCanvas.width = width; + dstCanvas.height = height; + var dstCtx; + if (alphaVal == 'true') + dstCtx = dstCanvas.getContext('bitmaprenderer', { alpha: true }); + else if (alphaVal == 'false') + dstCtx = dstCanvas.getContext('bitmaprenderer', { alpha: false }); + else + dstCtx = dstCanvas.getContext('bitmaprenderer'); + dstCtx.transferFromImageBitmap(image); + + var myCanvas = document.createElement('canvas'); + myCanvas.width = width; + myCanvas.height = height; + var myCtx = myCanvas.getContext('2d'); + myCtx.drawImage(dstCanvas, 0, 0); + testCanvas(myCtx, expectedR, expectedG, expectedB, expectedA); +} + +promise_test(function() { + var srcCanvas = document.createElement('canvas'); + srcCanvas.width = width; + srcCanvas.height = height; + var ctx = srcCanvas.getContext('2d'); + ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; + ctx.fillRect(0, 0, width, height); + createImageBitmap(srcCanvas).then(function(image) { + consumeImageBitmap(image, 'false', 0, 127, 0, 255); + }); +}, "Test that an ImageBitmapRenderingContext with alpha disabled makes the canvas opaque"); + +promise_test(function() { + var srcCanvas = document.createElement('canvas'); + srcCanvas.width = width; + srcCanvas.height = height; + var ctx = srcCanvas.getContext('2d'); + ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; + ctx.fillRect(0, 0, width, height); + createImageBitmap(srcCanvas).then(function(image) { + consumeImageBitmap(image, 'true', 0, 255, 0, 127); + }); +}, "Test that an ImageBitmapRenderingContext with alpha enabled preserves the alpha"); + +promise_test(function() { + var srcCanvas = document.createElement('canvas'); + srcCanvas.width = width; + srcCanvas.height = height; + var ctx = srcCanvas.getContext('2d'); + ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; + ctx.fillRect(0, 0, width, height); + createImageBitmap(srcCanvas).then(function(image) { + consumeImageBitmap(image, '', 0, 255, 0, 127); + }); +}, "Test that the 'alpha' context creation attribute is true by default"); + +</script> diff --git a/imagebitmap-renderingcontext/context-creation.html b/imagebitmap-renderingcontext/context-creation.html new file mode 100644 index 0000000..3daa397 --- /dev/null +++ b/imagebitmap-renderingcontext/context-creation.html
@@ -0,0 +1,19 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Canvas's ImageBitmapRenderingContext test</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context"> +<script> +var width = 10; +var height = 10; + +test(function() { + var canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + var ctx = canvas.getContext('bitmaprenderer'); + assert_true(ctx instanceof ImageBitmapRenderingContext); +}, "Test that canvas.getContext('bitmaprenderer') returns an instance of ImageBitmapRenderingContext"); + +</script> diff --git a/imagebitmap-renderingcontext/context-preserves-canvas.html b/imagebitmap-renderingcontext/context-preserves-canvas.html new file mode 100644 index 0000000..eca7afe --- /dev/null +++ b/imagebitmap-renderingcontext/context-preserves-canvas.html
@@ -0,0 +1,21 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Canvas's ImageBitmapRenderingContext test</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context"> +<script> +var width = 10; +var height = 10; + +test(function() { + var canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + var ctx = canvas.getContext('bitmaprenderer'); + var dstCanvas = ctx.canvas; + assert_equals(dstCanvas.width, width); + assert_equals(dstCanvas.height, height); +}, "Test that ctx.canvas on a ImageBitmapRenderingContext returns the original canvas"); + +</script> diff --git a/imagebitmap-renderingcontext/tranferFromImageBitmap-null.html b/imagebitmap-renderingcontext/tranferFromImageBitmap-null.html new file mode 100644 index 0000000..c12a8c9 --- /dev/null +++ b/imagebitmap-renderingcontext/tranferFromImageBitmap-null.html
@@ -0,0 +1,75 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Canvas's ImageBitmapRenderingContext test</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context"> +<script> +var width = 10; +var height = 10; + +function testCanvas(ctx, r, g, b, a) +{ + var color = ctx.getImageData(5, 5, 1, 1).data; + assert_array_equals(color, [r, g, b, a]); +} + +promise_test(function() { + function testTransferFromImageBitmapNullability(greenImage, redImage) { + var bitmapCanvas = document.createElement('canvas'); + bitmapCanvas.width = width; + bitmapCanvas.height = height; + var bitmapCtx = bitmapCanvas.getContext('bitmaprenderer'); + bitmapCtx.transferFromImageBitmap(greenImage); + + // Make sure the bitmap renderer canvas is filled correctly. + var myCanvas = document.createElement('canvas'); + myCanvas.width = width; + myCanvas.height = height; + var myCtx = myCanvas.getContext('2d'); + myCtx.drawImage(bitmapCanvas, 0, 0); + testCanvas(myCtx, 0, 255, 0, 255); + + // Test if passing null resets the bitmap renderer canvas. + // Drawing the resetted canvas cannot change the destination canvas. + bitmapCtx.transferFromImageBitmap(null); + var myCanvas2 = document.createElement('canvas'); + myCanvas2.width = width; + myCanvas2.height = height; + var myCtx2 = myCanvas2.getContext('2d'); + myCtx2.drawImage(bitmapCanvas, 0, 0); + testCanvas(myCtx2, 0, 0, 0, 0); + + // Test if we can redraw the bitmap canvas correctly after reset. + bitmapCtx.transferFromImageBitmap(redImage); + var myCanvas3 = document.createElement('canvas'); + myCanvas3.width = width; + myCanvas3.height = height; + var myCtx3 = myCanvas3.getContext('2d'); + myCtx3.drawImage(bitmapCanvas, 0, 0); + testCanvas(myCtx3, 255, 0, 0, 255); + } + + var greenCanvas = document.createElement('canvas'); + greenCanvas.width = width; + greenCanvas.height = height; + var greenCtx = greenCanvas.getContext('2d'); + greenCtx.fillStyle = '#0f0'; + greenCtx.fillRect(0, 0, width, height); + + var redCanvas = document.createElement('canvas'); + redCanvas.width = width; + redCanvas.height = height; + var redCtx = redCanvas.getContext('2d'); + redCtx.fillStyle = '#f00'; + redCtx.fillRect(0, 0, width, height); + + return Promise.all([ + createImageBitmap(greenCanvas), + createImageBitmap(redCanvas), + ]).then(([greenImage, redImage]) => { + testTransferFromImageBitmapNullability(greenImage, redImage); + }); +},'Test that transferFromImageBitmap(null) discards the previously transferred image'); + +</script> diff --git a/imagebitmap-renderingcontext/transferFromImageBitmap-detached.html b/imagebitmap-renderingcontext/transferFromImageBitmap-detached.html new file mode 100644 index 0000000..2c547ee --- /dev/null +++ b/imagebitmap-renderingcontext/transferFromImageBitmap-detached.html
@@ -0,0 +1,34 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Canvas's ImageBitmapRenderingContext test</title> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context"> +<script> +var width = 10; +var height = 10; + +promise_test(function() { + function testException(image) { + var dstCanvas = document.createElement('canvas'); + dstCanvas.width = width; + dstCanvas.height = height; + var dstCtx = dstCanvas.getContext('bitmaprenderer'); + dstCtx.transferFromImageBitmap(image); + + // The image should be detached after transferFromImageBitmap. + assert_equals(image.width, 0); + assert_equals(image.height, 0); + assert_throws("InvalidStateError", function() { dstCtx.transferFromImageBitmap(image); }); + } + + var srcCanvas = document.createElement('canvas'); + srcCanvas.width = width; + srcCanvas.height = height; + var ctx = srcCanvas.getContext('2d'); + ctx.fillStyle = 'rgba(0, 255, 0, 0.5)'; + ctx.fillRect(0, 0, width, height); + createImageBitmap(srcCanvas).then(testException); +}, "Test transferFromImageBitmap(image) with a detached image should throw InvalidStateError"); + +</script>